
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [string] $path = ".\OSList.csv",
    [string] $OSSrc = "..\Downloads",
    [string] $PSRoot = ((Resolve-Path $PSScriptRoot\..\Templates.Common\Distribution).Path),
    [string] $OSCache = "$(split-path -qualifier $PSRoot)\_cache"
)

$ErrorActionPreference = "Stop"

Write-Verbose "OsCache: $OSCache"
Write-Verbose "PSroot: $PSRoot"

###############################################

import-module "$env:programfiles\Microsoft Deployment Toolkit\Bin\MicrosoftDeploymentToolkit.psd1"

$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$WishList = import-csv $path

if ( -not ( Test-Path $OSCache )) { New-Item -Path $OSCache -ItemType Directory -Force | out-null}

if ( -not (get-psdrive -name DS002 -ErrorAction SilentlyContinue) ) 
{
    write-verbose "Load Provider $PSRoot"
    New-PSDrive -Name "DS002" -PSProvider "MDTProvider" -Root $PSRoot -Description "MDT Deployment Share" -Verbose | Out-String |Write-Verbose
}

###############################################

foreach ( $Type in "Create","Deploy","Upgrade")
{
    Remove-Item "DS002:\Operating Systems\$Type" -Recurse -Force -ErrorAction SilentlyContinue
    Remove-Item "DS002:\Task Sequences\$Type" -Recurse -Force -ErrorAction SilentlyContinue
}

###############################################

# TSID	Title	Index	Template	Type	Path

foreach ( $Item in $WishList ) 
{
    Write-Verbose ("#" * 80)
    $ITem | select -Property TSID,Title | out-string | Write-Verbose
    Write-Verbose "Find: $($Item.path)"
    if ( -not ( Test-Path "$OSCache\$($Item.OSFolder)\$($Item.TSID)\sources\install.wim"))
    {
        Write-Verbose "output: 'C:\Program Files\7-Zip\7z.exe' x '-o$OSCache\$($Item.OSFolder)\$($Item.TSID)' $OSSrc\$($Item.path)\*.iso"
        & 'C:\Program Files\7-Zip\7z.exe' x "-o$OSCache\$($Item.OSFolder)\$($Item.TSID)" "$OSSrc\$($Item.path)\*.iso"
        if ( $Item.WIMOveride )
        {
            Move-Item "$OSCache\$($Item.OSFolder)\$($Item.TSID)\Sources\Install.wim" "$OSCache\$($Item.OSFolder)\$($Item.TSID)\Sources\Install.Old.wim" -Force -ErrorAction SilentlyContinue
            Dism.exe /Export-Image "/SourceImageFile:$OSCache\$($Item.OSFolder)\$($Item.TSID)\Sources\Install.Old.wim" "/SourceIndex:$(1 + $Item.Index)" "/DestinationImageFile:$OSCache\$($Item.OSFolder)\$($Item.TSID)\Sources\Install.wim" /DestinationName:CapturedImage 
            Remove-Item "$OSCache\$($Item.OSFolder)\$($Item.TSID)\Sources\Install.Old.wim"
        }
    }

    if ( -not (test-path "DS002`:\Operating Systems\$($Item.OSFolder)"))
    {
        New-Item -ItemType Directory -Force -Path "DS002`:\Operating Systems\$($Item.OSFolder)" -erroraction silentlycontinue | out-string |Write-Verbose
    }

    if ( -not (test-path "DS002`:\Operating Systems\$($Item.OSFolder)\$($Item.TSID)"))
    {
        Write-Verbose "new path DS002`:\Operating Systems\$($Item.OSFolder)\$($Item.TSID)"
        New-Item -ItemType Directory -Force -Path "DS002`:\Operating Systems\$($Item.OSFolder)\$($Item.TSID)" | out-string |Write-Verbose
    }

    Write-Verbose "Import-MDTOperatingSystem -Path 'DS002`:\Operating Systems\$($Item.OSFolder)\$($Item.TSID)' -SourcePath '$OSCache\$($Item.OSFolder)\$($Item.TSID)' -DestinationFolder $($Item.OSFolder)\$($Item.TSID)"
    $OSes =        Import-MDTOperatingSystem -Path "DS002`:\Operating Systems\$($Item.OSFolder)\$($Item.TSID)" -SourcePath "$OSCache\$($Item.OSFolder)\$($Item.TSID)" -DestinationFolder "$($Item.OSFolder)\$($Item.TSID)"
    foreach ( $OS in $OSes ) 
    {
        $OS.Item('Name') += " - " + $Item.OSFolder
    }

    $OSEs | Out-String |Write-Verbose
    $SourceOS = $OSes
    if ( ($OSes |Measure-Object).count -gt 1 ) 
    {
        $OSes | Out-String | write-verbose
        $SourceOS = $OSes[$Item.Index]
    }
    $SourceOS | Out-String |Write-Verbose

    if ( -not (test-path "DS002`:\Task Sequences\$($Item.TSFolder)"))
    {
        New-Item -ItemType Directory -Force -Path "DS002`:\Task Sequences\$($Item.TSFolder)" -erroraction silentlycontinue | Out-String | Write-Verbose
    }

    Write-Verbose "Import-MDTTaskSequence -path 'DS002`:\Task Sequences\$($Item.TSFolder)' -template $($Item.template) -Hide $($Item.Hide) -ID $($Item.TSID).$($Item.OSFolder.substring(0,3)) -Name $($Item.Title)"
    $NewTS = import-MDTTaskSequence -Path "DS002`:\Task Sequences\$($Item.TSFolder)" -OperatingSystem $SourceOS -template $Item.template `
        -ID "$($Item.TSID).$($Item.OSFolder.substring(0,3))" -Name $Item.Title -FullName "Corporate User" -OrgName "Corporate IT Department" -HomePage "about:tabs"

    # $NewTS.Item('Hide') = $Item.Hide

}

Remove-PSDrive -Name DS002
Remove-Item $env:temp\Empty.wim -ErrorAction SilentlyContinue

Write-Verbose "done"
